﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;
using System.Web.Mvc;

namespace CSBackup_Web.Sources.Override
{
    #region 导入C++dll
    /// <summary>
    /// 备份功能导入
    /// </summary>
    public class BackupExport
    {
        /// <summary>
        /// 备份导出函数
        /// </summary>
        /// <param name="prjPath">工程路径</param>
        /// <param name="selected">待备份文件路径</param>
        /// <param name="backupType">备份类型</param>
        /// <param name="dependBackup">依赖的备份</param>
        /// <param name="granularity">增量或差分备份粒度</param>
        /// <param name="length">待备份文件路径数组长度</param>
        /// <returns></returns>
        [DllImport("SecureAutoBackup.dll", EntryPoint = "Backup", CharSet = CharSet.Unicode)]
        public extern static bool BackUp(string prjPath, string[] selected, int length, string backupType, string dependBackup, string granularity);
    }
    #endregion

    #region 使用BackgroundWorker重写备份线程，参见BackupWorker
    /// <summary>
    /// 备份线程类，用于控制备份线程，与界面进度条控件异步交互
    /// 在程序退出时，需检查线程数组，并确保所有线程停止
    /// 已不使用，备份线程使用BackupWorker
    /// </summary>
    /// <see cref="BackupWorker"/>
    //class BackupThread
    //{

    //    #region 线程所需参数
    //    //工程路径
    //    private string prjPath;
    //    //待备份文件
    //    private string selected;
    //    //备份类型
    //    private string backupType;
    //    //依赖的备份
    //    private string dependBackup;
    //    #endregion

    //    #region 线程和线程位置
    //    /// <summary>
    //    /// 线程类
    //    /// </summary>
    //    private Thread thread;
    //    #endregion

    //    #region 与界面交互
    //    /// <summary>
    //    /// 进度条所在窗口
    //    /// </summary>
    //    private System.Windows.Forms.Form mainForm;

    //    /// <summary>
    //    /// 进度条委托
    //    /// </summary>
    //    public delegate void ProgressIncHandler();

    //    /// <summary>
    //    /// 进度条事件
    //    /// </summary>
    //    public event ProgressIncHandler progressIncHandler;
    //    #endregion

    //    #region 属性
    //    /// <summary>
    //    /// 工程文件路径
    //    /// </summary>
    //    public string ProjectPath
    //    {
    //        set
    //        {
    //            this.prjPath = value;
    //        }
    //    }

    //    /// <summary>
    //    /// 待备份文件路径
    //    /// </summary>
    //    public string SelectedFilePath
    //    {
    //        set
    //        {
    //            this.selected = value;
    //        }
    //    }

    //    /// <summary>
    //    /// 备份类型
    //    /// </summary>
    //    public string BackupType
    //    {
    //        set
    //        {
    //            this.backupType = value;
    //        }
    //    }

    //    /// <summary>
    //    /// 依赖的备份名字（增量、查分备份需要）
    //    /// </summary>
    //    public string DependedBackup
    //    {
    //        set
    //        {
    //            this.dependBackup = value;
    //        }
    //    }
    //    #endregion

    //    #region 构造方法
    //    /// <summary>
    //    /// 备份线程构造方法
    //    /// </summary>
    //    /// <param name="mainForm">进度条所在窗体</param>
    //    public BackupThread(System.Windows.Forms.Form mainForm)
    //    {
    //        this.mainForm = mainForm;
    //    }
    //    #endregion

    //    #region 线程控制和功能方法
    //    /// <summary>
    //    /// 备份线程功能函数
    //    /// </summary>
    //    private void DoBackup()
    //    {
    //        if (BackupExport.BackUp(this.prjPath, this.selected, this.selected.Length,this.backupType, this.dependBackup))
    //        {
    //            if (this.progressIncHandler != null && this.mainForm != null)
    //            {
    //                this.mainForm.BeginInvoke(this.progressIncHandler);
    //            }
    //        }
    //    }

    //    /// <summary>
    //    /// 启动线程
    //    /// </summary>
    //    public void Start()
    //    {
    //        this.thread = new Thread(new ThreadStart(DoBackup));
    //        this.thread.Start();
    //    }

    //    /// <summary>
    //    /// 停止线程
    //    /// </summary>
    //    public void Stop()
    //    {
    //        this.thread.Abort();
    //    }
    //    #endregion
    //}
    #endregion

    #region 使用单独专用线程进行备份
    /// <summary>
    /// 使用单独专用线程进行备份
    /// </summary>
    class BackupWorker
    {
        #region 委托声明
        /// <summary>
        /// 进度条处理委托声明
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void Worker_ProgressChanged(Object sender, ProgressChangedEventArgs e);

        public Worker_ProgressChanged worker_ProgressChanged = null;

        /// <summary>
        /// 恢复完成，刷新列表事件委托声明
        /// </summary>
        public delegate ActionResult Worker_ProgressComplete();
        public Worker_ProgressComplete worker_ProgressComplete = null;
        #endregion

        #region 成员
        private BackgroundWorker worker = null;
        private string prjPath = null;
        private List<string> paths = null;
        private string backupType = null;
        private string dependBackup = null;
        private string granularity = null;
        #endregion

        #region 属性
        /// <summary>
        /// 设置进度条处理函数
        /// </summary>
        public Worker_ProgressChanged Worker_ProgressChangedEventHandler
        {
            set
            {
                this.worker_ProgressChanged = null;
                this.worker_ProgressChanged += value;
                worker.ProgressChanged += new ProgressChangedEventHandler(value);
            }
        }

        /// <summary>
        /// 设置恢复完成事件处理
        /// </summary>
        public Worker_ProgressComplete Worker_ProgressCompleteEventHandler
        {
            set
            {
                this.worker_ProgressComplete = null;
                this.worker_ProgressComplete += value;
            }
        }
        #endregion

        #region 构造方法
        /// <summary>
        /// 创建备份后台实例
        /// </summary>
        /// <param name="prjPath">工程路径信息</param>
        /// <param name="paths">选中的待备份文件</param>
        /// <param name="backupType">备份文件类型</param>
        /// <param name="dependBackup">依赖的备份路径</param>
        /// <param name="granularity">增量或差分备份粒度</param>
        public BackupWorker(string prjPath, List<string> paths, string backupType, string dependBackup,string granularity)
        {
            worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += new DoWorkEventHandler(doWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            this.prjPath = prjPath;
            this.paths = paths;
            this.backupType = backupType;
            this.dependBackup = dependBackup;
            this.granularity = granularity;
        }
        #endregion

        #region 备份
        /// <summary>
        /// 线程执行逻辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void doWork(Object sender, DoWorkEventArgs e)
        {
            //int percentComplete =
             // (int)((float)(this.paths.Count / (float)4) / (float)this.paths.Count * 100);
            worker.ReportProgress(25);   
            BackupExport.BackUp(this.prjPath, this.paths.ToArray(),this.paths.Count, this.backupType, this.dependBackup,this.granularity);
            worker.ReportProgress(100);  
        }

        /// <summary>
        /// 专用线程执行完毕事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void worker_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
        {
            //Report: "备份成功！"
            this.worker_ProgressComplete();
        }

        /// <summary>
        /// 启动线程执行
        /// </summary>
        public void RunWorkerAsync()
        {
            this.worker.RunWorkerAsync();
        }
        #endregion
    }
    #endregion

}
